//
//  TransRootViewController.m
//  test
//
//  Created by kepuG on 17/2/27.
//  Copyright © 2017年 test. All rights reserved.
//

#import "TransRootViewController.h"
#import "GGPicBrowsePage.h"
#import "transModel.h"


@interface TransRootViewController ()<UICollectionViewDelegate ,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong ,nonatomic) NSMutableArray *dataArr;



@end

@implementation TransRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"hello"];
    self.dataArr = [NSMutableArray array];
    for (int i = 0; i<90; i++) {
        transModel *model = [[transModel alloc] init];
        
        [self.dataArr addObject:model];
    }
    [self.collectionView reloadData];
    
    // Do any additional setup after loading the view from its nib.
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}


// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hello" forIndexPath:indexPath];
    

    transModel *model = [self.dataArr objectAtIndex:indexPath.item];
    
    model.cell = cell;
    
    
   // DebugLog(@"=========>>>>>>>>>>%@",NSStringFromCGRect(cellInSuperview));
    
    cell.backgroundColor = [UIColor redColor];
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DebugLog(@"====>>>>>>>>");
    
    /**取到模型中的cell*/
    transModel *model = [self.dataArr objectAtIndex:indexPath.item];

    /**计算cell相对window的位置*/
    CGRect cellInCollection = [self.collectionView convertRect:model.cell.frame toView:self.collectionView];
    CGRect cellInSuperview = [self.collectionView convertRect:cellInCollection toView:self.view];
    
    /**截屏取到cell的view 并且设置位置*/
    UIView *view = [model.cell snapshotViewAfterScreenUpdates:NO];
    view.frame = cellInSuperview;
    
    GGPicBrowsePage *detailVC = [[GGPicBrowsePage alloc] initWithParentVC:self contentView:view];
    
    [detailVC show];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
